from recreator import Recreator
from neutron.manager import NeutronManager
from neutron_lib import context as ncontext
from neutron_lib.plugins import directory


class Builder(object):
    """Build data struct for resources"""

    def __init__(self):
        # have to do it
        if not directory.is_loaded():
            manager = NeutronManager.get_instance()

        self.lbaas_plugin = directory.get_plugin(
            "LOADBALANCERV2")
        self.recreator = Recreator(
            self.lbaas_plugin)

    def rebuild_loadbalancers(self, ids):
        context = ncontext.get_admin_context()
        for _id in ids:
            lb = self.lbaas_plugin.db.get_loadbalancer(
                context, id=_id)
            self.recreator.recreate_loadbalancer(
                context, lb
            )

    def rebuild_listeners(self, ids):
        context = ncontext.get_admin_context()
        for _id in ids:
            ls = self.lbaas_plugin.db.get_listener(
                context, id=_id
            )
            self.recreator.recreate_listener(
                context, ls
            )

    def rebuild_pools(self, ids):
        context = ncontext.get_admin_context()
        for _id in ids:
            pl = self.lbaas_plugin.db.get_pool(
                context, id=_id
            )
            self.recreator.recreate_pool(
                context, pl
            )

    def rebuild_members(self, ids):
        context = ncontext.get_admin_context()
        for _id in ids:
            mb = self.lbaas_plugin.db.get_pool_member(
                context, id=_id
            )
            self.recreator.recreate_member(
                context, mb
            )
